{extend name="admin@index/index"}

{block name='style'}
<!--<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />-->
<link rel="stylesheet" href="__ROOT__/static/theme/css/login.css">
{/block}

{block name="body"}
<div id="wrap">
    <video id="video" src="" autoplay loop muted>
        您的浏览器不支持 video 标签。
    </video>
    <p id="bg" class="image"><img src=""></p>
    <div class="mask"></div>
    <div class="module">
        <ul>
            <div class="head_box">
                <p class="logo"><img  class="image" width="80" height="80" src="{:sysconf('logo')}" alt=""><span class="title">{:sysconf('site_name')}</span></p>
                <div class="tab">
                    <span class="tab_item on">账号密码登录</span>
                </div>
            </div>
            <li class="tab_content">
                <form action="">
                    <div class="box">
                        <i class="layui-icon layui-icon-username"></i>
                        <input id="user" type="text" placeholder="请输入账号" autocomplete="off" {if config('app_debug')}value=""{/if}>
                    </div>
                    <div class="box">
                        <i class="layui-icon layui-icon-password"></i>
                        <input id="password" type="password" placeholder="请输入密码" autocomplete="off" {if config('app_debug')}value=""{/if}>
                    </div>
                    <!--{if !config('app_debug')}-->
                    <div class="box">
                        <i class="layui-icon layui-icon-vercode"></i>
                        <input id="img_code" type="text" placeholder="请输入图形验证码" autocomplete="off">
                        <div class="img_code img"><img src="{:url('captcha')}" alt="" onclick="this.src='{:url(\'captcha\')}?seed='+Math.random()"></div>
                    </div>
                    <!--{/if}-->
                    <input class="btn_block" type="submit" value="登录">
                </form>
            </li>
        </ul>
    </div>
    <div class="footer notselect">
        <p class="layui-hide-xs"><a target="_blank" href="https://www.google.cn/chrome">推荐使用谷歌浏览器</a></p>
        {:sysconf('site_copy')}
        {if sysconf('miitbeian')}<span class="padding-5">|</span><a target="_blank" href="http://beian.miit.gov.cn">{:sysconf('miitbeian')}</a>{/if}
    </div>
</div>
{/block}

{block name='script'}
<script>
    $(function(){
        if (location.href.indexOf('#') > -1){
            location.replace(location.href.split('#')[0]);
            return false;
        }
        // 视频或图片url
        var url = "{:sysconf('login_bgm')}";
        if (/^(\s|\S)+(jpg|png|JPG|PNG)+$/.test(url)) {
            // 图片
            $("#video").remove();
            $("#bg").show().find("img").attr("src",url);
        }else{
            // 视频
            $("#bg").remove();
            $("#video").attr("src",url);
            $("#video")[0].play();
        }
        // 切换登录方式
        $("#wrap").on("click",".tab .tab_item",function(){
            if(!$(this).hasClass("on")){
                $(this).addClass("on").siblings(".on").removeClass("on");
                var index = $(this).prevAll(".tab_item").length;
                $("#wrap .tab_content").hide().eq(index).show();
            }
        });
        // 获取验证码
        $("#getCode").on("click", function () {
            var $this = $(this);
            if ($this.hasClass("isSend")) {
                return false;
            }
            var phone = $("#phone").val();
            if (!/^1[3456789]\d{9}/.test(phone)) {
                // 提示
                layer.msg("请输入正确的手机号");
                return false;
            }
            $this.text("发送中...").addClass("isSend");
            $.ajax({
                type: "post",
                url: "{:url('admin/login/sendAdminLoginSms')}",
                data: {'phone':phone},
                dataType: "json",
                success: function (res) {
                    // 发送成功
                    if(res.code == 1){
                        var num = 60;
                        $this.text(num+"s后重新获取");
                        var time = setInterval(function () {
                            num--;
                            if (num == 0) {
                                $this.text("获取验证码").removeClass("isSend");
                                clearInterval(time);
                                return false
                            }
                            $this.text(num+"s后重新获取");
                        }, 1000)
                    }else{
                        $this.text("获取验证码").removeClass("isSend");
                        layer.msg(res.info);return false;
                    }
                },
                error: function(){
                    layer.msg("发送失败");
                    $this.text("获取验证码").removeClass("isSend");
                }
            });
        });
        // 登录
        $("#wrap").on("submit","form",function(){
            var index = $("#wrap .tab .on").prevAll(".tab_item").length;
            if(index==0){
                // 账号密码登录
                var user = $("#user").val();
                var password = $("#password").val();
                var img_code = $("#img_code").val();
                if(!user){
                    layer.msg("请输入账号");
                    return false;
                }
                if (!password) {
                    layer.msg("请输入密码");
                    return false;
                }
                lay = layer.open({type:3,icon:1});
                $.ajax({
                    type: "post",
                    url: location.href,
                    data: {
                        username: user,
                        password: password,
                        verifyCode: img_code
                    },
                    dataType: "json",
                    success: function (res) {
                        if(res.code == 1){
                            layer.msg(res.info,{
                                icon:1,
                                time:2000,
                                end:function(){
                                    // location.href=res.data;
                                    location.reload();
                                }
                            });
                        }else{
                            layer.close(lay);
                            layer.msg(res.info);return false;
                        }
                    }
                });
            }else{
                // 手机验证码登录
                var phone = $("#phone").val();
                var code = $("#code").val();
                if (!(/^1[3456789]\d{9}$/).test(phone)) {
                    layer.msg("请输入正确的手机号");
                    return false;
                }
                if (!code) {
                    layer.msg("输入验证码");
                    return false;
                }
                lay = layer.open({type:3,icon:1});
                $.ajax({
                    type: "post",
                    url: "{:url('admin/login/adminSmsLogin')}",
                    data: {
                        phone: phone,
                        code: code
                    },
                    dataType: "json",
                    success: function (res) {
                        if(res.code == 1){
                            layer.msg(res.info,{
                                icon:1,
                                time:2000,
                                end:function(){
                                    location.reload();
                                }
                            });
                        }else{
                            layer.close(lay);
                            layer.msg(res.info);return false;
                        }
                    }
                });
            }
            return false;
        });
    })
</script>
{/block}
